package com.example.calendertodo;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;

import com.example.calendertodo.util.TodoDbOpenHelper;
import com.haibin.calendarview.Calendar;
import com.haibin.calendarview.MonthView;

public class MyMonthView extends MonthView {

    Paint paint;
    TodoDbOpenHelper mTodoDbOpenHelper;
    private String currentCalendar;
    public MyMonthView(Context context) {
        super(context);
        paint = new Paint();
        mTodoDbOpenHelper = new TodoDbOpenHelper(context);

        java.util.Calendar calendarUtil = java.util.Calendar.getInstance();
        int currentYear = calendarUtil.get(java.util.Calendar.YEAR);
        int currentMonth = calendarUtil.get(java.util.Calendar.MONTH)+1;
        int currentDay = calendarUtil.get(java.util.Calendar.DAY_OF_MONTH);
        currentCalendar = currentYear + "-" + format(currentMonth) + "-" + format(currentDay);
    }

    @Override
    protected boolean onDrawSelected(Canvas canvas, Calendar calendar, int x, int y, boolean hasScheme) {
        int mRadius = Math.min(mItemWidth, mItemHeight) / 2;
        int cx = x + mItemWidth / 2;
        int cy = y + mItemHeight / 2;
        canvas.drawCircle(cx, cy, mRadius, mSelectedPaint);
        return true;
    }

    @Override
    protected void onDrawScheme(Canvas canvas, Calendar calendar, int x, int y) {

    }

    @Override
    protected void onDrawText(Canvas canvas, Calendar calendar, int x, int y, boolean hasScheme, boolean isSelected) {

        int cx = x + mItemWidth / 2;
        int top = y - mItemHeight / 6;

        canvas.drawText(String.valueOf(calendar.getDay()), cx, mTextBaseLine + top,
                calendar.isCurrentDay() ? mCurDayTextPaint :
                        calendar.isCurrentMonth() ? mCurMonthTextPaint : mOtherMonthTextPaint);

        String year = calendar.toString().substring(0,4);
        String month = calendar.toString().substring(4,6);
        String day = calendar.toString().substring(6,8);
        String exactCalendar = year + "-" + month + "-" + day;

        //如果有任务
        if (hasTask(exactCalendar)) {
            //未完成
            if (hasUnfinishedTask(exactCalendar)) {
                paint.setColor(Color.WHITE);
            }
            //完成
            else {
                paint.setColor(getResources().getColor(R.color.blue_1));
            }
            canvas.drawCircle(cx, mTextBaseLine + y + mItemHeight / 10, 10, paint);
        }
    }
    public boolean hasTask(String calendar){

        int sow = mTodoDbOpenHelper.queryByDate(calendar.toString(),"3");
        if (sow>0){
            return true;
        }
        return false;
    }

    private boolean hasUnfinishedTask(String calendar){

        int sow = mTodoDbOpenHelper.queryByDateAndState(calendar.toString(),"0");
        if (sow > 0){
            return true;
        }
        return false;
    }

    public String format(int i) {
        String time = "";
        if(i <= 9) {
            time = "0" + i;
        }else {
            time = i + "";
        }
        return time;
    }
}
